package com.wyd.filter;

import com.wyd.entity.Context;
import com.wyd.entity.DseProductPriorityInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Fact;
import org.jeasy.rules.annotation.Rule;

import java.util.Iterator;

@Rule(name = "user_weight_filter", description = "用户权益过滤", priority = 1)
@Slf4j
public class UserWeightFilter {

  @Condition
  public boolean when() {
    return true;
  }

  @Action
  public void action(@Fact("ct") Context ct) {
     log.info("开始执行>>>用户权益过滤");

     if(ct.getDseProductList() == null || ct.getDseProductList().isEmpty()){
       return;
     }

     //条件1：存在权益禁止产品列表且产品存在于禁止列表里面则过滤
     if(ct.getForbidDseProductIdList() != null && ct.getForbidDseProductIdList().size() > 0) {
       Iterator<DseProductPriorityInfo> iterator = ct.getDseProductList().iterator();
       while (iterator.hasNext()){
         DseProductPriorityInfo info = iterator.next();
         if(ct.getForbidDseProductIdList().contains(info.getDseProductId())){
           iterator.remove();
           log.info("用户权益禁止产品过滤产品id>>>{}",info.getDseProductId());
         }
       }
     }

     //条件2：未存在优先产品列表内且unsettledAssignStatus字段设置为默认禁止（0）则过滤
     if(ct.getUnsettledAssignStatus() != null && ct.getUnsettledAssignStatus() == 0){
       Iterator<DseProductPriorityInfo> iterator = ct.getDseProductList().iterator();
       while (iterator.hasNext()){
         DseProductPriorityInfo info = iterator.next();
         if(ct.getPriorityDseProductIdList() ==null || !ct.getPriorityDseProductIdList().contains(info.getDseProductId())){
           iterator.remove();
           log.info("用户权益禁止产品过滤产品id>>>{}",info.getDseProductId());
         }
       }
     }

  }
}
